package com.woniu.aimd.service.impl;

import cn.hutool.json.JSONUtil;
import com.woniu.aimd.pojo.entity.CommentData;
import com.woniu.aimd.pojo.entity.MergedCommentDocument;
import com.woniu.aimd.mapper.MergedCommentMapper;
import com.woniu.aimd.service.MergedCommentService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MergedCommentServiceImpl implements MergedCommentService {

    @Resource
    private MergedCommentMapper commentMapper;

    public void saveComments(String noteId, String userId, List<CommentData> comments) {
        String json = JSONUtil.toJsonStr(comments);
        MergedCommentDocument doc = new MergedCommentDocument();
        doc.setNoteId(noteId);
        doc.setUserId(userId);
        doc.setComments(json);
        commentMapper.upsert(doc);
    }

    public List<CommentData> loadComments(String noteId, String userId) {
        MergedCommentDocument doc = commentMapper.selectOne(noteId, userId);
        if (doc == null) return List.of();
        return JSONUtil.toList(JSONUtil.parseArray(doc.getComments()), CommentData.class);
    }
}
